home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / bootpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  20.4 KB  |  760 lines

  1. /*
  2.   Last updated : Mon Sep 16 15:20:57 1996
  3.   Modified by JSP from code by Charles Hawkins <ceh@eng.cam.ac.uk>,
  4.  
  5.     J.S.Peatfield@damtp.cam.ac.uk
  6.  
  7.   Copyright (c) University of Cambridge, 1993,1994,1995,1996
  8.  
  9.   $Revision: 1.2 $
  10.   $Date: 1997/02/26 20:25:02 $
  11. */
  12.  
  13. /* Standard headers */
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/time.h>
  19. #include <netinet/in.h>
  20. #include <netinet/ip.h>
  21. #include <netinet/ip_icmp.h>
  22. #include <netdb.h>
  23. #include <arpa/inet.h>
  24. #include <net/if.h>
  25. #include <net/if_arp.h>
  26. #include <sys/time.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <ctype.h>
  32.  
  33. /* local headers */
  34. #include "bootpc.h"
  35.  
  36. /* My global variables */
  37. int bootp_verbose = 0 ;   /* verbose mode or not 10/02/94 JSP */
  38. int bootp_debug   = 0 ;   /* debug mode or not 14/02/94 JSP */
  39.  
  40. static int returniffail ;  /* Return to the user if we fail */
  41. static int printflag;      /* Print control */
  42. static int sockfd;
  43.  
  44. int performBootp(char *device,
  45.          char *server,
  46.          char *bootfile,
  47.          int timeout_wait,
  48.          int givenhwaddr,
  49.          struct ifreq *their_ifr,
  50.          int waitformore,
  51.          int bp_rif,
  52.          int bp_pr)
  53.  
  54. {
  55.   struct ifreq ifr;
  56.   struct sockaddr_in cli_addr, serv_addr;
  57.   struct bootp *bootp_xmit, *bootp_recv;
  58.   fd_set rfds, wfds, xfds;
  59.   struct timeval timeout ;
  60.   int32 rancopy ;
  61.   int cookielength ;
  62.   long plen ;
  63.   int retry_wait, waited=0 ;
  64.   int one=1, i ;
  65.   struct timeval tp;
  66.   int received_packet = 0 ;
  67. /* See RFC1497, RFC1542  09/02/94   JSP  */
  68.   unsigned char mincookie[] = {99,130,83,99,255} ;
  69.  
  70.   returniffail=bp_rif ;
  71.   printflag=bp_pr ;
  72.  
  73. /* zero structure before use */
  74.   memset((char *) &serv_addr, 0, sizeof(serv_addr));
  75.  
  76.   serv_addr.sin_family = AF_INET;
  77.   serv_addr.sin_addr.s_addr = inet_addr(server) ;
  78.   serv_addr.sin_port = htons(IPPORT_BOOTPS);
  79.  
  80.   if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  81.     logMessage("bootpc: socket failed %s", strerror(errno));
  82.     return BootpFatal();
  83.   }
  84.   
  85.   if (setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&one,sizeof(one))==-1) {
  86.     logMessage("bootpc: setsockopt failed %s", strerror(errno));
  87.     return BootpFatal();
  88.   }
  89.   
  90.   memset((char *) &cli_addr, 0, sizeof(cli_addr));
  91.   cli_addr.sin_family = AF_INET;
  92.   cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  93.   cli_addr.sin_port = htons(IPPORT_BOOTPC);
  94.  
  95.   if(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
  96.     logMessage("bootpc: bind failed %s", strerror(errno));
  97.     return BootpFatal();
  98.   }
  99.  
  100. /* allocate bootp packet before we use it */
  101.   bootp_xmit = (struct bootp *) malloc(BUFSIZ) ;
  102.   memset((char *) bootp_xmit, 0, BUFSIZ) ;
  103.  
  104.   bootp_recv = (struct bootp *) malloc(BUFSIZ) ;
  105.   memset((char *) bootp_recv, 0, BUFSIZ) ;
  106.  
  107. /* Don't do this if we were given the MAC address to use.  27/09/94  JSP */
  108.   if (givenhwaddr) {
  109.     /* If I'm spoofing my HW address then have the reply broadcast */
  110.     bootp_xmit->bp_flags |= BPFLAG_BROADCAST;
  111.     /* Assuming ETHER if given HW */
  112.     ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER ;
  113.     ifr.ifr_hwaddr = their_ifr->ifr_hwaddr ;
  114.   } else {
  115. /* Get the hardware address, and family information */
  116.     memcpy(ifr.ifr_name, device, strlen(device)+1);
  117.     if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
  118.       logMessage("bootpc: ioctl failed %s", strerror(errno));
  119.       return BootpFatal();
  120.     }
  121.   }
  122.  
  123. /* Check the network family if in NET3 or later, before NET3 you couldn't
  124.    examine this info (as far as I know.) */
  125.  
  126. /* set the htype field from the sa.family of the hardware address, if
  127.    this doesn't work for your interface type let me know please. */
  128.  
  129.   bootp_xmit->bp_htype = ifr.ifr_hwaddr.sa_family;
  130.   bootp_xmit->bp_hlen = IFHWADDRLEN ;  /* All MAC addresses are the same length */
  131.   
  132.   if (bootp_debug)
  133.     logMessage("Got family=%d (Ether=%d)",
  134.            bootp_xmit->bp_htype, ARPHRD_ETHER);
  135.  
  136. /* If we have the time seed with it xor the hardware address, otherwise
  137.    use the hardware address 12/02/94 JSP */
  138.   if (gettimeofday(&tp, NULL) == -1)
  139.     rancopy = 0 ;
  140.   else
  141.     rancopy = tp.tv_sec + tp.tv_usec ;
  142.  
  143. /* Do the XOR */
  144.   for (i=0; i < IFHWADDRLEN ; ++i) {
  145.     ((unsigned char *)&rancopy)[ i % sizeof(rancopy) ] ^=
  146.       ((unsigned char *)(ifr.use_hwaddr))[i] ;
  147.   }
  148. /* and set the seed */
  149.   srand(rancopy) ;
  150.  
  151.   if(bootp_debug) {
  152.     logMessage("hardware addr is :") ;
  153.     for (i=0; i < bootp_xmit->bp_hlen ; ++i)
  154.       logMessage("%2.2X ", ((unsigned char *)(ifr.use_hwaddr))[i]) ;
  155.   }
  156.  
  157. /* Now fill in the packet. */
  158.   bootp_xmit->bp_op = BOOTREQUEST ;
  159.  
  160. /* Now with my understanding of the bootp protocol we *should* just
  161.    need to copy the hwaddr over, but it seems that at least ARCNET
  162.    bootb servers are wird in this respect.  So here is a switch in
  163.    case of other weirdness.  JSP */
  164.  
  165.   switch(bootp_xmit->bp_htype) {
  166. /* ARCNET uses a "fake" ethernet address, with the ARCNET address at
  167.    the wrong end.  At least the Novell bootp server on ARCNET assumes
  168.    this.  Thanks to Tomasz Motylewski <motyl@tichy.ch.uj.edu.pl> for
  169.    reporting this.  */
  170.   case ARPHRD_ARCNET :
  171.     memcpy(bootp_xmit->bp_chaddr+IFHWADDRLEN-1, (char *)(ifr.use_hwaddr), 1) ;
  172.     bootp_xmit->bp_htype=ARPHRD_ETHER;
  173.     bootp_xmit->bp_hlen=IFHWADDRLEN;
  174.     break ;
  175.  
  176. /* Add other network weirdness here */
  177.  
  178. /* For sensible networks the rest is normal */
  179.   default :
  180.     memcpy(bootp_xmit->bp_chaddr,
  181.        (char *)(ifr.use_hwaddr),
  182.        bootp_xmit->bp_hlen) ;
  183.   }
  184.  
  185. /* Must start with zero here, see RFC1542 09/02/94 JSP */
  186.   bootp_xmit->bp_secs = 0;
  187.  
  188. /* Put in the minimal RFC1497 Magic cookie 09/02/94 JSP */
  189.   memcpy(bootp_xmit->bp_vend, mincookie, sizeof(mincookie));
  190.  
  191. /* Put the user precified bootfile name in place 12/02/94 */
  192.   memcpy(bootp_xmit->bp_file, bootfile, strlen(bootfile)+1);
  193.  
  194. /* put a random value in here, but keep a copy to check later 09/02/94  JSP */
  195.   bootp_xmit->bp_xid = rancopy = rand() ;
  196.  
  197.   retry_wait = 2 ;
  198.   if (bootp_verbose)
  199.     logMessage("BOOTPclient broadcast...");
  200.  
  201.   while (((waited <= timeout_wait) && !received_packet) ||
  202.      ((waited <= waitformore) && received_packet)) {
  203.     
  204.     if (!received_packet) {  /* Move this to a sendpacket function */
  205.       /* set time of this timeout  09/02/94  JSP */
  206.       bootp_xmit->bp_secs = waited ;
  207.       if (bootp_verbose) {
  208.     logMessage("."); fflush(stderr);
  209.       }
  210.       if (bootp_debug) {
  211.     logMessage("Size = %ld", (long)sizeof(struct bootp)) ;
  212.       }
  213.  
  214.       if(sendto(sockfd, bootp_xmit, sizeof(struct bootp), 0, 
  215.         (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  216.     logMessage("bootpc: sendto %s", strerror(errno));
  217.     return BootpFatal();
  218.       }
  219.     }
  220.  
  221.     /* Move rest of this loop to a receivepacket function */
  222.     FD_SET(sockfd,&rfds);
  223.     FD_ZERO(&wfds);
  224.     FD_ZERO(&xfds);
  225.  
  226. /* Randomise the delays a little as suggested in RFC1542  09/02/94  JSP */
  227.     timeout.tv_sec = retry_wait + (1+(rand() & (retry_wait-1))) ;
  228.     timeout.tv_usec = 0;
  229.     waited += timeout.tv_sec ;  /* Add this to the total time we have waited */
  230.  
  231.     if(select(sockfd+1, &rfds, &wfds, &xfds, &timeout)<0) {
  232.       logMessage("bootpc: select %s", strerror(errno));
  233.       return BootpFatal();
  234.     }
  235.  
  236.     if(!FD_ISSET(sockfd, &rfds)) {
  237.       retry_wait = retry_wait*2;
  238.     } else {
  239.       if ((plen = recvfrom(sockfd, bootp_recv, BUFSIZ, 0,
  240.                (struct sockaddr *)NULL, (int *)NULL)) < 0){
  241.     logMessage("bootpc: recvfrom %s", strerror(errno));
  242.     return BootpFatal();
  243.       }
  244.  
  245.       if (bootp_debug) {
  246.     logMessage("plen = %ld  plen - sizeof(struct bootp) = %ld",
  247.            (long)plen, (long)(plen - sizeof(struct bootp))) ;
  248.       }
  249.       cookielength = 64 + plen - sizeof(struct bootp) ;
  250.       
  251. /* Check xid is what we asked for  09/02/94  JSP */
  252.       if (bootp_recv->bp_xid != rancopy) {
  253.     logMessage("WARNING bp_xid mismatch got 0x%lx sent 0x%lx",
  254.            (long)bootp_recv->bp_xid, (long)rancopy) ;
  255.     if (!bootp_debug) /* Unless bootp_debugging, ignore packet */
  256. /* WRONG, but we need to do something fatal, we actually
  257.    want to go back to the receive part again to listen
  258.    for more packets */
  259.       exit(1) ;
  260.       }
  261.  
  262.       if (!received_packet) {
  263.     /* If we haven't already recieved a packet then set the time to wait
  264.        further to be now + time user specified */
  265.     waitformore += waited ;
  266.     received_packet = 1 ;
  267.       } else {
  268.     /* To make it look a bit prettier */
  269.     if (printflag & BP_PRINT_OUT)
  270.       printf ("\n") ;
  271.       }
  272.  
  273. /* Pass the cookie info, the mincookie to look for and our address to
  274.    the cookie parser.  It needs our address to get the network and
  275.    broadcast bits right if the SUBNET is defined in the cookie.
  276.    10/02/94  JSP */
  277.       ParsePacket(bootp_recv,
  278.           cookielength,
  279.           mincookie) ;
  280.     }
  281.   }
  282.   if (!received_packet) {
  283.     logMessage("No response from BOOTP server");
  284.     return BootpFatal();
  285.   }
  286.  
  287.   if (sockfd)
  288.     close (sockfd) ;
  289.   return 0 ;  /* Normal exit */
  290. }
  291.     
  292. int BootpFatal()
  293. {
  294.   if (sockfd)
  295.     close (sockfd) ;
  296.  
  297.   if (bootp_debug)
  298.     logMessage("In BootpFatal(), errno was %d", errno) ;
  299.  
  300.   if (returniffail) {
  301.     logMessage("bootpc failed to locate a network address") ;
  302.     return 1 ;
  303.   }
  304.  
  305.   logMessage(" Unable to locate an IP address for this host.\n"
  306.          "     ***Please report this problem**\n\n"
  307.          "          [Unable to continue]\n");
  308.  
  309.   if (bootp_debug)
  310.     logMessage("Will now loop forerver, break out of this to fix") ;
  311.  
  312.   while(1) {
  313.     /* your eyes are getting heavy.... */
  314.     sleep(1000) ;
  315.   }
  316. }
  317.  
  318. /* Parse Magic cookies as specified in RFC1497, well only the bits we
  319.    are actually interested in...  09/02/94 JSP
  320. */
  321. void ParsePacket(struct bootp * bootp_recv,
  322.          int cookielength,
  323.          unsigned char *match)
  324. {
  325.   int i=0, len, tag ;
  326.   int subnet = 0 ;
  327.   struct in_addr temp ;
  328.   unsigned char *cookie = (unsigned char *)(bootp_recv->bp_vend) ;
  329.   struct in_addr temp_addr, my_addr ;
  330.  
  331.   temp_addr.s_addr = bootp_recv->bp_siaddr.s_addr ;
  332.   OutString("SERVER", (unsigned char *)inet_ntoa(temp_addr), -1);
  333.   my_addr.s_addr = bootp_recv->bp_yiaddr.s_addr ;
  334.   OutString("IPADDR", (unsigned char *)inet_ntoa(my_addr), -1);
  335.   if (bootp_verbose) {
  336.     logMessage("bp_file len is %d", strlen(bootp_recv->bp_file)) ;
  337.   }    
  338.   OutString("BOOTFILE",
  339.           (unsigned char *)bootp_recv->bp_file, -1) ;
  340.  
  341.   if (bootp_debug) {  /* dump cookie contents in HEX 10/02/94  JSP */
  342.     for (i=0; i<cookielength; i++) {
  343.       if ((i%8) == 0)
  344.     logMessage("\n %2.2d :", i) ;
  345.       logMessage(" 0x%2.2X", cookie[i]) ;
  346.     }
  347.     logMessage("") ;
  348.   }
  349.  
  350. /* Must get the same cookie back as we sent  09/02/94  JSP */
  351.   for (i=0; i < 4; ++i) {
  352.     if (cookie[i] != match[i]) {
  353.       if (bootp_verbose)
  354.     logMessage("RFC1497 Cookie mismatch at offset %d", i) ;
  355.       return ;
  356.     }
  357.   }
  358.  
  359.   if (bootp_verbose)
  360.     logMessage("found valid RFC1497 cookie, parsing...") ;
  361.  
  362. /* Carry on after the cookie for other data  09/02/94  JSP */
  363.   while (i < cookielength) {
  364.     tag = cookie[i] ;
  365.  
  366.     if (bootp_verbose)
  367.       logMessage("cookie position %d is %d", i, tag) ;
  368.  
  369. /* If we arn't at the end of the cookie and we will need it extract len */
  370.     if ((i < cookielength - 1) && (tag != TAG_PAD) && (tag != TAG_END))
  371.       len = cookie[i+1] ;
  372.     else
  373.       len = 0 ;
  374.  
  375. /* Warn if the "length" takes us out of the cookie and truncate */
  376.     if (len + i > cookielength) {
  377.       if (bootp_verbose)
  378.     logMessage("TAG %d at %d.  len %d, overrun %d",
  379.            cookie[i], i, len, i + len - cookielength) ;
  380.       /* And truncate in any case even with no warning */
  381.       len = cookielength - i ;
  382.     }
  383.  
  384.     switch (cookie[i]) {  /* The monster switch statement ... */
  385. /* PAD cookie */
  386.     case TAG_PAD :
  387.       i++ ;
  388.       break ;
  389.  
  390. /* SUBNET we are in */
  391.     case TAG_SUBNET_MASK :
  392.       if (bootp_verbose && len != 4)
  393.     logMessage("WARNING len of tag 1 is %d not 4", len) ;
  394.       memcpy((char *)&temp, cookie + i + 2, 4) ;
  395.       OutString("NETMASK", (unsigned char *)inet_ntoa(temp), -1) ;
  396.  
  397. /* Both values are in network order so this doesn't care about the
  398.    ordering 10/02/94 JSP */
  399.       my_addr.s_addr &=  temp.s_addr ;
  400.       OutString("NETWORK", (unsigned char *)inet_ntoa(my_addr), -1) ;
  401.       my_addr.s_addr |= ~temp.s_addr ;
  402.       OutString("BROADCAST", (unsigned char *)inet_ntoa(my_addr), -1) ;
  403.  
  404. /* defined so we know later that subnet info has been printed 11/02/94  JSP */
  405.       subnet = 1 ;
  406.       i += len + 2 ;
  407.       break ;
  408.  
  409. /* Time of day */
  410.     case TAG_TIME_OFFSET :
  411.       /* ignored */
  412.       i += len + 2 ;
  413.       break ;
  414.  
  415. /* IP Gateways (routers) */
  416.     case TAG_GATEWAY :
  417.       OutList("GATEWAYS", cookie+i+2, len) ;
  418.       i += len + 2 ;
  419.       break ; 
  420.  
  421. /* Timeservers (see RFC-868) */
  422.     case TAG_TIME_SERVER :
  423.       OutList("TIMESRVS", cookie+i+2, len) ;
  424.       i += len + 2 ;
  425.       break ; 
  426.  
  427. /* IEN-116 Nameservers */
  428.     case TAG_NAME_SERVER :
  429.       OutList("IEN116SRVS", cookie+i+2, len) ;
  430.       i += len + 2 ;
  431.       break ; 
  432.  
  433. /* DNS Nameservers */
  434.     case TAG_DOMAIN_SERVER :
  435.       OutList("DNSSRVS", cookie+i+2, len) ;
  436.       i += len + 2 ;
  437.       break ; 
  438.  
  439. /* LOGGING servers */
  440.     case TAG_LOG_SERVER :
  441.       OutList("LOGSRVS", cookie+i+2, len) ;
  442.       i += len + 2 ;
  443.       break ; 
  444.  
  445. /* Quote of day/Cookie servers */
  446.     case TAG_COOKIE_SERVER :
  447.       OutList("QODSRVS", cookie+i+2, len) ;
  448.       i += len + 2 ;
  449.       break ; 
  450.  
  451. /* LPR servers */
  452.     case TAG_LPR_SERVER :
  453.       OutList("LPRSRVS", cookie+i+2, len) ;
  454.       i += len + 2 ;
  455.       break ; 
  456.  
  457. /* Impress (Imogen) servers */
  458.     case TAG_IMPRESS_SERVER :
  459.       OutList("IMPRESSSRVS", cookie+i+2, len) ;
  460.       i += len + 2 ;
  461.       break ; 
  462.  
  463. /* Remote Location Protocol servers */
  464.     case TAG_RLP_SERVER :
  465.       OutList("RLPSRVS", cookie+i+2, len) ;
  466.       i += len + 2 ;
  467.       break ; 
  468.  
  469. /* HOSTNAME (may be fqdn or leaf) */
  470.     case TAG_HOST_NAME :
  471.       OutString("HOSTNAME", cookie+i+2, len) ;
  472.       i += len + 2 ;
  473.       break ;
  474.  
  475. /* BOOT File Size (ignored) */
  476.     case TAG_BOOT_SIZE :
  477.       i += len + 2 ;
  478.       break ;
  479.  
  480. /* Merit DUMP File name (ignored) */
  481.     case TAG_DUMP_FILE :
  482.       i += len + 2 ;
  483.       break ;
  484.  
  485. /* DOMAIN */
  486.     case TAG_DOMAIN_NAME :
  487.       OutString("DOMAIN", cookie+i+2, len) ;
  488.       OutSearch("SEARCH", cookie+i+2, len) ;
  489.       i += len + 2 ;
  490.       break ;
  491.  
  492. /* SWAPServer address */
  493.     case TAG_SWAP_SERVER :
  494.       OutList("SWAPSRVR", cookie+i+2, len) ;
  495.       i += len + 2 ;
  496.       break ;
  497.  
  498. /* Root pathname to mount as root filesystem  */
  499.     case TAG_ROOT_PATH :
  500.       OutString("ROOT_PATH", cookie+i+2, len) ;
  501.       i += len + 2 ;
  502.       break ;
  503.  
  504. /* Extensions.  Name of further Cookie data */
  505.     case TAG_EXTEN_FILE :
  506.       OutString("EXTEN_FILE", cookie+i+2, len) ;
  507.       i += len + 2 ;
  508.       break ;
  509.  
  510. /* NIS (formerly YP) domain name */
  511.     case TAG_NIS_DOMAIN :
  512.       OutString("YPDOMAIN", cookie+i+2, len) ;
  513.       i += len + 2 ;
  514.       break ;
  515.        
  516. /* NIS (formerly YP) server */
  517.     case TAG_NIS_SERVER :
  518.       OutList("YPSRVR", cookie+i+2, len) ;
  519.       i += len + 2 ;
  520.       break ;
  521.        
  522. /* Time servers */
  523.     case TAG_NTP_SERVER :
  524.       OutList("NTPSRVS", cookie+i+2, len) ;
  525.       i += len + 2 ;
  526.       break ;
  527.  
  528. /* END of cookie (phew) */
  529.     case TAG_END :
  530.       if (bootp_verbose)
  531.     logMessage("end of cookie parsing, END tag found") ;
  532.       return ;
  533.  
  534.     default:
  535.       { char name[30] ;
  536.     if (bootp_verbose) {
  537.       if (tag >= 128 && tag <= 254) /* reserved */
  538.         logMessage("Reserved TAG %d at %d (len %d)", tag, i, len) ;
  539.       else
  540.         logMessage("Unknown TAG %d at %d (len %d)", tag, i, len) ;
  541.     }
  542.     sprintf(name, "T%3.3d", tag) ;
  543.     OutString(name, cookie+i+2, len) ;
  544.     i += 2 + len ;
  545.       }
  546.       break ;
  547.     }
  548.   }
  549.  
  550.   /* No SUBNET TAG in the cookie so we fake guess here, if this is wrong
  551.      then fix your bootp server to tell us the answer rather than
  552.      hacking this code. */
  553.  
  554.   if (!subnet) {
  555.     struct in_addr netmask ;
  556.     int type ;
  557.     
  558.     if (bootp_verbose)
  559.       logMessage("Guessing netmask from IP address range") ;
  560.  
  561.     type = ntohl(temp_addr.s_addr) ;
  562.     if ((type & 0x80000000) == 0) {
  563.       /* Class A */
  564.       netmask.s_addr = htonl(0xFF000000) ;
  565.     } else if ((type & 0x40000000) == 0) {
  566.       /* Class B */
  567.       netmask.s_addr = htonl(0xFFFF0000) ;
  568.     } else if ((type & 0x20000000) == 0) {
  569.       /* Class C */
  570.       netmask.s_addr = htonl(0xFFFFFF00) ;
  571.     } else { /* GOD KNOWS... other classes are weird */
  572.       if (bootp_verbose)
  573.     logMessage("IP number not Class A,B or C. Setting NETMASK to zero") ;
  574.       netmask.s_addr = htonl(0x00000000) ;
  575.     }
  576.     OutString("NETMASK", (unsigned char *)inet_ntoa(netmask), -1);
  577.     temp_addr.s_addr &= netmask.s_addr ;
  578.     OutString("NETWORK", (unsigned char *)inet_ntoa(temp_addr), -1);
  579.     temp_addr.s_addr |= ~netmask.s_addr ;
  580.     OutString("BROADCAST", (unsigned char *)inet_ntoa(temp_addr), -1);
  581.   }
  582. }
  583.  
  584.  
  585. /* Print out a list of IP addresses */
  586. void OutList(char *name,
  587.            unsigned char *cookie,
  588.            int len)
  589. {
  590.   struct in_addr temp ;
  591.   char lenv[BUFSIZ], *ptr ;
  592.   int n, c, i;
  593.  
  594.   if (bootp_verbose)
  595.     logMessage("%s found len=%d", name, len) ;
  596.  
  597.   if ((len % 4) != 0) {
  598.     if (bootp_verbose)
  599.       logMessage("ERROR %s length (%d) not 4 div", name, len) ;
  600.     return ;
  601.   }
  602.   if (len == 0) /* Nothing to do  10/02/94  JSP */
  603.     return ;
  604.  
  605.   for (n=0,i=1 ; len; len -= 4, cookie += 4, i++) {
  606.     char lbuf[BUFSIZ] ;
  607.     memcpy((char *)&temp, cookie, 4) ;
  608.     ptr = inet_ntoa(temp) ;
  609.     c = strlen(ptr) ;
  610.     sprintf(lbuf, "%s_%d", name, i) ;
  611.     OutString(lbuf, (unsigned char *)ptr, c) ;
  612.     strncpy(lenv+n, ptr, c) ;
  613.     n += c ;
  614.     if (len > 4)
  615.       lenv[n++] = ' ';
  616.   }
  617.   lenv[n] = 0 ;
  618.  
  619.   doOut(name, lenv) ;
  620. }
  621.  
  622. /* Prints the string passed */
  623. void OutString(char *name,
  624.            unsigned char *cookie,
  625.            int len)
  626. {
  627.   char lenv[BUFSIZ];
  628.   if (len == -1)
  629.     len = strlen((char *)cookie) ;
  630.  
  631.   safecopy((unsigned char *)lenv, cookie, len);
  632.   doOut(name, lenv) ;
  633. }
  634.  
  635. /* Prints the string as usable in a DNS search.  This is doing the
  636.    same as the old default BIND (pre 4.9.3) did with a DOMAIN line,
  637.    for backwards compatibility, and since BOOTP doesn't allow a way to
  638.    specify the search path explicitly */
  639. void OutSearch(char *name,
  640.          unsigned char *cookie,
  641.          int len)
  642. {
  643.   unsigned char *ptr, *nptr ;
  644.   unsigned char buf[258] ;  /* Max len is 255 */
  645.   char lenv[BUFSIZ] ;
  646.   int n=0;
  647.  
  648.   strncpy((char *)buf, (char *)(cookie), len) ;
  649.   buf[ len + 1 ] = 0 ;  /* Null terminate it */
  650.   ptr = buf ;
  651.  
  652.   while (len) {
  653.     safecopy((unsigned char *)(lenv+n), ptr, len) ;
  654.     n += len ;
  655.     /* Goto next bit */
  656.     nptr = (unsigned char *)strchr((char *)ptr, '.') ;  /* Cast cast cast */
  657.     if (nptr == NULL) {
  658.       len = 0 ; /* End of string I hope */
  659.     } else {
  660.       if (strchr((char *)nptr + 1, '.') == NULL) {
  661.     /* Trad to not use last component */
  662.     len = 0 ;
  663.       } else {
  664.     len -= (nptr - ptr) + 1 ;
  665.     ptr = nptr + 1 ;
  666.     lenv[n++] = ' ' ;
  667.       }
  668.     }
  669.   }
  670.   lenv[n] = 0 ;
  671.  
  672.   doOut(name, lenv) ;
  673. }
  674.  
  675. /* Takes an address and returns useful bits of the name after lookup,
  676.    this was a seperate program, but it is more compact to have both
  677.    together.  17/02/94  JSP */
  678.  
  679. int in2host(char *address,
  680.         int bp_pr)
  681. {
  682.   struct in_addr sin_addr;
  683.   struct hostent *hp;
  684.   char *c ;
  685.  
  686.   printflag=bp_pr ;
  687.  
  688. /* convert to standard network form */
  689.   sin_addr.s_addr = inet_addr(address);
  690.  
  691. /* perform lookup, must have DNS running or have local hosts file at
  692.    this point */
  693.  
  694.   hp = gethostbyaddr((char *)&sin_addr, sizeof(sin_addr), AF_INET) ;
  695.  
  696.   if (hp == NULL) {
  697.     logMessage ("bootpc: gethostbyaddr %s", strerror(errno)) ;
  698.     return -1;
  699.   }
  700.  
  701. /* Print out a known name to stop repeated calls */
  702.   OutString("DONEIN2HOST",(unsigned char *)"1", -1) ;
  703.  
  704. /* Print out full name as returned by the call */
  705.   OutString("HOSTFULL", (unsigned char *)(hp->h_name), -1) ;
  706.  
  707.   for(c=(char *)hp->h_name; *c ; ++c)
  708.     if(*c == '.') {
  709. /* Zap first 'dot' to give leaf and domain names */
  710.       OutString("HOSTDOMAIN", (unsigned char *)(c+1), -1) ;
  711.       OutSearch("HOSTSEARCH", (unsigned char *)(c+1), strlen(c+1)) ;
  712.       *c = 0 ;
  713.       OutString("HOSTLEAF", (unsigned char *)(hp->h_name), -1) ;
  714.       return 0 ;
  715.     }
  716.   return 0 ;
  717. }
  718.  
  719. /* Copy those bits of a string which are alphanumeric or in a
  720.    "safe" list of characters. */
  721. void safecopy(unsigned char *out,
  722.           unsigned char *string,
  723.           int len)
  724. {
  725.   char safe[] = "./:-_=+[]~()%&*^#@! " ;
  726.   int i, c ;
  727.  
  728.   for (i =0 ; i < len; ++i) {
  729.     c = string[i] ;
  730.     if (isalnum(c))
  731.       out[i] = c ;   /* alphanumeric */
  732.     else {  /* Not alphanumeric */
  733.       if (strchr(safe, c) != NULL) {
  734.     out[i] = c ; /* but safe */
  735.       } else {
  736.     out[i] = '?' ; /* NOT safe */
  737.     if (bootp_verbose)
  738.       logMessage("Illegal char 0x%2.2X", c) ;
  739.       }
  740.     }
  741.   }
  742.   out[i] = 0 ;
  743. }
  744.  
  745. void doOut(char *name,
  746.        char *lenv)
  747. {
  748.   if (printflag & BP_PRINT_OUT) {
  749.     printf("%s='%s'\n", name, lenv) ;
  750.   }
  751.   if (printflag & BP_PUT_ENV) {
  752.     char envb[BUFSIZ], *envp ;
  753.     sprintf(envb, "BOOTP_%s=%s", name, lenv) ;
  754.     envp = strdup(envb) ;
  755.     if (bootp_debug)
  756.       logMessage("ENV setting :%s:", envp) ;
  757.     putenv(envp) ;
  758.   }
  759. }
  760.